package cn.tedu.csmall.passport.config;

import cn.tedu.csmall.passport.filter.JwtAuthorizationFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Slf4j
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Autowired
    JwtAuthorizationFilter jwtAuthorizationFilter;

    public SecurityConfiguration() {
        log.debug("加载配置类：SecurityConfiguration");
    }
    @Bean
    public PasswordEncoder passwordEncoder() {
        log.debug("创建密码编码器：PasswordEncoder");
        return new BCryptPasswordEncoder();
    }
    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        //访问白名单
        String [] urls={
               "/favicon.ico",
               "/doc.html",
               "/**/*.js",
               "/**/*.css",
               "/swagger-resources/**",
               "/v2/api-docs",
                "/admin/login"
       };
        http.cors();//允许跨域访问,
        http.csrf().disable();//禁用防止跨域攻击

        http.authorizeRequests()//要求请求必须被授权
                .antMatchers(urls)//匹配一些路径
                .permitAll()//允许访问
                .anyRequest()//除以上配置以外的请求
                .authenticated();//经过认证的
//        http.formLogin();//启动登录表单，未授权的请求均会重定向到登录表单
        //将"JWT过滤器"添加在“认证过滤器”之前
        http.addFilterBefore(jwtAuthorizationFilter,
                UsernamePasswordAuthenticationFilter.class);
    }
}
